home *** CD-ROM | disk | FTP | other *** search
/ Network Support Library / RoseWare - Network Support Library.iso / apidev / os2spx.arc / KNUTCODE.OCT next >
Text File  |  1989-07-27  |  5KB  |  177 lines

  1. /*
  2.     SPX sample program
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <malloc.h>
  7. #include <ipxcalls.h>
  8. #include <spxcalls.h>
  9. #include <spxerror.h>
  10. #define INCL_BASE
  11. #include <os2.h>
  12. #include <nwcalls.h>
  13. #include <doscalls.h>
  14.  
  15. #define STACK_SIZE 4000        /*  Stack space for each thread        */
  16. #define PACKET_NUM 50        /*  Number of packets to send        */
  17.  
  18. BYTE    NetAddress[4];        /*  Space for Node and Network address    */
  19. BYTE    NodeAddress[6];        /*  Keep these two together!        */
  20. LONG    quit_sem_val = 0;
  21. HSEM    quit_sem = &quit_sem_val;
  22.  
  23. main()
  24. {
  25.     void far receiver();        /*  Address of receiver            */
  26.     void far sender();            /*  Address of sender            */
  27.     TID thread_ID;                /*  Thread ID                    */
  28.     WORD far *thread_stack_ptr;    /*  Pointer to thread's stack    */
  29.     WORD socket_num;            /*  Socket number                */
  30.     BYTE internet_address[10];    /*  Net and Node addresses        */
  31.  
  32.     printf("SPX test program:\n");
  33.     printf("  Send and receive %d packets\n\n", PACKET_NUM);
  34.  
  35.     /*  Obtain current network and node addresses  */
  36.     IpxGetInternetworkAddress(internet_address);
  37.     memmove(NetAddress, internet_address, sizeof(NetAddress));
  38.     memmove(NodeAddress, &internet_address[4], sizeof(NodeAddress));
  39.  
  40.     /*  Open socket for use by both ends of connection  */
  41.     socket_num = 0;
  42.     SpxOpenSocket(&socket_num);
  43.  
  44.     /*  Set the semaphore so main knows when to quit  */
  45.     DosSemSet(quit_sem);
  46.  
  47.     /*  Spin off the thread to do receives  */
  48.     thread_stack_ptr = (int far *) malloc(STACK_SIZE);
  49.     thread_stack_ptr += (STACK_SIZE / sizeof(WORD));
  50.     *(--thread_stack_ptr) = socket_num;
  51.     DosCreateThread(receiver, &thread_ID, (char far *) thread_stack_ptr);
  52.  
  53.     /*  Spin off the thread to do sends  */
  54.     thread_stack_ptr = (int far *) malloc(STACK_SIZE);
  55.     thread_stack_ptr += (STACK_SIZE / sizeof(WORD));
  56.     *(--thread_stack_ptr) = socket_num;
  57.     DosCreateThread(sender, &thread_ID, (char far *) thread_stack_ptr);
  58.  
  59.     /*  Wait until the packets are received  */
  60.     DosSemWait(quit_sem, -1L);
  61.  
  62.     /*  Close the socket before exiting  */
  63.     SpxCloseSocket(socket_num);
  64.     printf("*** Done sending packets\n");
  65.  
  66.     /*  Exit and kill the child threads  */
  67.     DosExit(1, 0);
  68. }
  69.  
  70.  
  71. void far receiver(WORD socket_num)
  72. {
  73.     /*  Variables for receiving packets  */
  74.     SPX_ECB ecb;                /*  ECB                    */
  75.     BYTE retryCount = 0;        /*  retry count            */
  76.     BYTE watchDog = 0;            /*  watch dog            */
  77.     WORD conn;                    /*  connection number    */
  78.     SPX_HEADER spxhdr;            /*  SPX headers            */
  79.     LONG sem_val = 0;            /*  RAM                    */
  80.     HSEM sem = &sem_val;        /*   semaphore            */
  81.     int packet_buf;                /*  packet area            */
  82.     int i;                        /*  loop count            */
  83.  
  84.     /*  ECB information for Listen end  */
  85.     ecb.fragCount = 1;
  86.     ecb.fragList[0].fragAddress = &spxhdr;
  87.     ecb.fragList[0].fragSize = sizeof(spxhdr);
  88.     ecb.reserved1 = (LONG) sem;
  89.  
  90.     /*  Set the semaphore, post the listen, and wait  */
  91.     DosSemSet(sem);
  92.     SpxListenForConnection(socket_num, &ecb, retryCount, watchDog, &conn);
  93.     DosSemWait(sem, -1L);
  94.  
  95.     printf("*** Receiver connected\n");
  96.     printf("*** Begin sending packets\n");
  97.  
  98.     /*  Receive packets   */
  99.     for (i = 0; i < PACKET_NUM; i++) {
  100.         /*  Clear area to receive the packet  */
  101.         packet_buf = 0;
  102.  
  103.         /*  ECB stuff for Receive end  */
  104.         ecb.fragCount = 2;
  105.         ecb.fragList[0].fragAddress = &spxhdr;
  106.         ecb.fragList[0].fragSize = sizeof(spxhdr);
  107.         ecb.fragList[1].fragAddress = &packet_buf;
  108.         ecb.fragList[1].fragSize = sizeof(packet_buf);
  109.         ecb.reserved1 = (LONG) sem;
  110.  
  111.         /*  Set the semaphore, post the listen, and wait for packet  */
  112.         DosSemSet(sem);
  113.         SpxListenForSequencedPacket(socket_num, &ecb);
  114.         DosSemWait(sem, -1L);
  115.         printf(".");
  116.     }
  117.     /*  Receiver done sending packets  */
  118.     printf("\n");
  119.     DosSemClear(quit_sem);
  120.     DosExit(0, 0);
  121. }
  122.  
  123.  
  124. void far sender(WORD socket_num)
  125. {
  126.     SPX_ECB ecb;            /*  ECB            */
  127.     BYTE retryCount = 0;        /*  retry count        */
  128.     BYTE watchDog = 0;        /*  watch dog        */
  129.     WORD conn;            /*  connection number    */
  130.     SPX_HEADER spxhdr;        /*  SPX headers        */
  131.     HSYSSEM sem;            /*  System semaphore    */
  132.     int i;
  133.  
  134.     /*  ECB stuff for Establish end  */
  135.     ecb.fragCount = 1;
  136.     ecb.fragList[0].fragAddress = &spxhdr;
  137.     ecb.fragList[0].fragSize = sizeof(spxhdr);
  138.     DosCreateSem(1, &sem, "\\SEM\\SPXTEST\\EXAMPLE.SPX");
  139.     ecb.reserved1 = (LONG) sem;
  140.  
  141.     /*  SPX Header stuff  */
  142.     memmove(&(spxhdr.destNet), NetAddress, sizeof(NetAddress));
  143.     memmove(spxhdr.destNode, NodeAddress, sizeof(NodeAddress));
  144.     spxhdr.destSocket = socket_num;
  145.  
  146.     /*  Set the semaphore, establish, and wait  */
  147.     DosSemSet(sem);
  148.     SpxEstablishConnection(socket_num, &ecb, retryCount, watchDog, &conn);
  149.     DosSemWait(sem, 1000L);
  150.     printf("*** Sender connected\n");
  151.  
  152.     /*  Send packets   */
  153.     for (i = 0; i < PACKET_NUM; i++) {
  154.         /*  ECB stuff for Send end  */
  155.         ecb.fragCount = 2;
  156.         ecb.fragList[0].fragAddress = &spxhdr;
  157.         ecb.fragList[0].fragSize = sizeof(spxhdr);
  158.         ecb.fragList[1].fragAddress = &i;
  159.         ecb.fragList[1].fragSize = sizeof(i);
  160.         ecb.reserved1 = (LONG) sem;
  161.  
  162.         /*  SPX Header stuff for Send end */
  163.         memmove(&(spxhdr.destNet), NetAddress, sizeof(NetAddress));
  164.         memmove(spxhdr.destNode, NodeAddress, sizeof(NodeAddress));
  165.         spxhdr.destSocket = socket_num;
  166.         spxhdr.connectionCtl = 0;
  167.         spxhdr.dataStreamType = 0;
  168.  
  169.         /*  Set the semaphore, send the packet, and wait  */
  170.         DosSemSet(sem);
  171.         SpxSendSequencedPacket(conn, &ecb);
  172.         DosSemWait(sem, -1L);
  173.     }
  174.     /*  Sender is done sending packets  */
  175.     DosExit(0, 0);
  176. }
  177.